package com.koron.css2.projectCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.clientCenter.mapper.OpenUserMapper;
import com.koron.css2.projectCenter.bean.PjInstallBean;
import com.koron.css2.projectCenter.mapper.PjConsultMapper;
import com.koron.css2.projectCenter.mapper.PjInstallMapper;
import com.koron.css2.projectCenter.service.PjInstallService;
import com.koron.css2.projectCenter.vo.PjMeterVO;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;

/**
 * 用水安装保存
 * @author 许海宜
 * 2020年4月7日
 */
public class PjInstallSave implements ServerInterface {

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		try {
			//将入参转换
			PjInstallBean bean = JsonUtils.objectToPojo(req.getData(), PjInstallBean.class);

			//检查主键
			if(StringUtils.isBlank(bean.getId())) {
				return MessageBean.create(Constant.MESSAGE_INT_FAIL, "主键不能为空。", null);
			}
			//检查流程状态
			String nodeCode = bean.getProcessState();
			if(!StringUtils.equalsAny(nodeCode,PjInstallService.BUILD_NODE, PjInstallService.CHECK_NODE, PjInstallService.OPEN_NODE, PjInstallService.PRESETMT_NODE, PjInstallService.SETMT_NODE)) {
				return MessageBean.create(Constant.MESSAGE_INT_FAIL, "流程状态不存在。", null);
			}
			
			//节点检查
			String checkResult = checkNode(factory, bean, nodeCode);
			if(!"OK".equals(checkResult)) {
				return MessageBean.create(Constant.MESSAGE_INT_FAIL, checkResult, null);
			}
			
			//执行保存操作
			int result = savePjInstall(factory, userInfo, bean);
			if(result == 0) {
				return MessageBean.create(Constant.MESSAGE_INT_FAIL, "保存失败，单据不存在或流程节点已发生改变。", null);
			}
			
			//输出结果
			return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "保存成功", null);
		} catch (Exception e) {
			factory.close(false);
			logger.error("用水安装保存失败"+e.getMessage(), e);
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "保存失败", null);
		}
	}
	
	/**
	 * 节点检查
	 */
	private String checkNode(SessionFactory factory, PjInstallBean bean, String nodeCode) {
		//施工
		if(StringUtils.equals(nodeCode, PjInstallService.BUILD_NODE)) {
			if(StringUtils.isBlank(bean.getSendOrderDate())) {
				return "派单日期不能为空。";
			}
			
			if(StringUtils.isBlank(bean.getConstGroup())) {
				return "施工人员不能为空。";
			}
			if(StringUtils.isBlank(bean.getConstBeginDate())) {
				return "施工起始日期不能为空。";
			}
			if(StringUtils.isBlank(bean.getConstBeginDate())) {
				return "施工结束日期不能为空。";
			}
		}
		//验收
		if(StringUtils.equals(nodeCode, PjInstallService.CHECK_NODE)) {
			if(StringUtils.isBlank(bean.getCheckStaff())) {
				return "验收人员不能为空。";
			}
			if(StringUtils.isBlank(bean.getCheckDate())) {
				return "验收日期不能为空。";
			}
		}
		//立户
		if(StringUtils.equals(nodeCode, PjInstallService.OPEN_NODE)) {
			//检查多水表信息的完整性
			PjConsultMapper mapper = factory.getMapper(PjConsultMapper.class);
			List<PjMeterVO> list = mapper.meterList(bean.getConsultId());
			for (PjMeterVO vo : list) {
				if(StringUtils.isBlank(vo.getOpenDate())) {
					return "立户日期不能为空。";
				}
				if(StringUtils.isBlank(vo.getMeterNo())) {
					return "水表编号不能为空。";
				}
				if(StringUtils.isBlank(vo.getSetupMeterAddr())) {
					return "装表地址不能为空。";
				}
				if(StringUtils.isBlank(vo.getSetupMeterLocation())) {
					return "装表位置不能为空。";
				}
				if(StringUtils.isBlank(vo.getSetupMeterDate())) {
					return "装表日期不能为空。";
				}
				if(StringUtils.isBlank(vo.getBookNo())) {
					return "册本号不能为空。";
				}
				if(StringUtils.isBlank(vo.getMeterReadingCycle())) {
					return "抄表周期不能为空。";
				}
				if(StringUtils.isBlank(vo.getUseWaterType())) {
					return "用水类型不能为空。";
				}
			}
			
			/*
			 * PjConsultMapper pjConsultMapper = factory.getMapper(PjConsultMapper.class);
			 * PjConsultBean pjConsultBean =
			 * pjConsultMapper.selectById(bean.getConsultId()); ProjectReceiptMapper pmapper
			 * = factory.getMapper(ProjectReceiptMapper.class); int total =
			 * pmapper.totalCount(bean.getId());
			 * if("2".equals(pjConsultBean.getPrepayType()) && total==1) {
			 * 
			 * }
			 */
			 
		}
		//编制结算
		if(StringUtils.equals(nodeCode, PjInstallService.PRESETMT_NODE)) {
			if(StringUtils.isBlank(bean.getSetmtStaff())) {
				return "结算人员不能为空。";
			}
			if(StringUtils.isBlank(bean.getSetmtDate())) {
				return "结算日期不能为空。";
			}
		}
		//工程结算
		if(StringUtils.equals(nodeCode, PjInstallService.SETMT_NODE)) {
			//检查累计金额与已收费收款单合计是否相同  银川版本不检查
			/*
			ProjectReceiptMapper mapper = factory.getMapper(ProjectReceiptMapper.class);
			
			
			PjInstallMapper installMapper = factory.getMapper(PjInstallMapper.class);
			PjInstallBean pjInstallBean = installMapper.selectById(bean.getId());
			Double total = mapper.totaleq(pjInstallBean.getBillNo());
			if(Double.doubleToLongBits(total==null?0:total) != Double.doubleToLongBits(pjInstallBean.getAccruedMoney()==null?0.0:pjInstallBean.getAccruedMoney())) {
				return "存在未收款费用，请收款后再操作。";
			}*/
		}
		return "OK";
	}

	/**
	 * 保存用水安装
	 * @param factory
	 * @param userInfo
	 * @param bean
	 * @return
	 */
	private int savePjInstall(SessionFactory factory, UserInfoBean userInfo, PjInstallBean bean) {
		String nodeCode = bean.getProcessState();
		bean.setUpdateName(userInfo.getUserInfo().getName());
		bean.setUpdateAccount(userInfo.getUserInfo().getAcount());
		PjInstallMapper mapper = factory.getMapper(PjInstallMapper.class);
		OpenUserMapper openMapper = factory.getMapper(OpenUserMapper.class);
		//施工
		if(StringUtils.equals(nodeCode, PjInstallService.BUILD_NODE)) {
			return mapper.saveBuild(bean);
		}
		//验收
		if(StringUtils.equals(nodeCode, PjInstallService.CHECK_NODE)) {
			return mapper.saveCheck(bean);
		}
		//立户
		if(StringUtils.equals(nodeCode, "Process-task13")) {
			
		}
		//编制结算
		if(StringUtils.equals(nodeCode, PjInstallService.PRESETMT_NODE)) {
			
			if(bean.getSetmtMoney()!=null && bean.getSetmtMoney()>0) {
				bean.setBackFillMoney(bean.getSetmtMoney()-(bean.getPrepayMoney()==null?0:bean.getPrepayMoney()));
			}else {
				bean.setSetmtMoney(
						(bean.getSetmtLaborMeterAfter()==null?0:bean.getSetmtLaborMeterAfter()) +
						(bean.getSetmtLaborMeterBefore()==null?0:bean.getSetmtLaborMeterBefore())+
						(bean.getSetmtMatrMeterAfter()==null?0:bean.getSetmtMatrMeterAfter())+
						(bean.getSetmtMatrMeterBefore()==null?0:bean.getSetmtMatrMeterBefore())+
						(bean.getSetmtOtherMeter()==null?0:bean.getSetmtOtherMeter())+
						(bean.getSetmtDesignCost()==null?0:bean.getSetmtDesignCost())+
						(bean.getCostShare()==null?0:bean.getCostShare())
				);
				bean.setBackFillMoney(bean.getSetmtMoney()-(bean.getPrepayMoney()==null?0:bean.getPrepayMoney()));
			}
			return mapper.saveSetmt(bean);
		}
		//工程结算
		if(StringUtils.equals(nodeCode, PjInstallService.SETMT_NODE)) {
			//结算日期为空时数据库字段不兼容，设置为null
			if(bean!=null && "".equals(bean.getSetmtPayDate())) {
				bean.setSetmtPayDate(null);
			}
			return mapper.saveRefund(bean);
		}
		return 1;
	}
}
